﻿using Infrastructure.DDD;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Application.Entities
{
    /*
     * CURD、启用、暂停、日志
     */
    [Table("job_jobTask")]
    public class JobTask : Entity
    {
        [Column(TypeName = "varchar(100)")]
        public string Title { get; set; }

        [Column(TypeName = "text")]
        public string Content { get; set; }

        [Column(TypeName = "varchar(100)")]
        public string Cron { get; set; }
        [Column(TypeName = "varchar(500)")]
        public string NoticeObjects { get; set; }
        public long CreatorId { get; set; }
        public bool IsEnable { get; set; }
        public DateTime? LastExecutionTime { get; set; }
        public DateTime? NextExecutionTime { get; set; }
        public bool? LastExecutionStatus { get; set; }
        [Column(TypeName = "varchar(200)")]
        public string Expected { get; set; }
        [Column(TypeName = "varchar(200)")]
        public string Note { get; set; }
        [Column(TypeName = "varchar(100)")]
        public string Group { get; set; }
        [Column(TypeName = "varchar(20)")]
        public string RequestMethond { get; set; }
        [Column(TypeName = "text")]
        public string RequestHeader { get; set; }
        [Column(TypeName = "text")]
        public string RequestBody { get; set; }
        [Column(TypeName = "varchar(500)")]
        public string FailureRetryPolicy { get; set; }
        public int RandomDelay { get; set; }
        public long ServerId { get; set; }
        public int LogRetentionDays { get; set; }
        [Column(TypeName = "varchar(500)")]
        public string Result { get; set; }
        [Column(TypeName = "varchar(20)")]
        public string RecentExecutionStatus { get; set; } = "";//最近执行状态：1成功0失败，记录最近10次的
        /// <summary>
        /// 已执行次数
        /// </summary>
        public int ExecutionCount { get; set; }
        public NoticePolicy NoticePolicy { get; set; }
        public DateTime? LastFailureTime { get; set; }
        public DateTime? LastSuccessTime { get; set; }
        [Column(TypeName = "varchar(20)")]
        public JobType JobType { get; set; }
        [Comment("是否被调度")]
        public bool IsScheduled { get; set; }
        public long? PreJobId { get; set; }
        public long? NextJobId { get; set; }
    }
    /// <summary>
    /// 状态
    /// </summary>
    public enum JobStatus
    {
        /// <summary>
        /// 正常
        /// </summary>
        [Display(Name = "启用")]
        Enable = 0,
        /// <summary>
        /// 停用
        /// </summary>
        [Display(Name = "停用")]
        Disable = 1,
    }

    public enum NoticePolicy
    {
        [Display(Name = "仅异常通知")]
        OnlyException = 0,
        [Display(Name = "异常或恢复时通知")]
        ExceptionOrRecovery = 1,
        [Display(Name = "一律不通知")]
        Never = 2,
        [Display(Name = "一律通知")]
        All = 3,
    }

    public enum JobType
    {
        [Display(Name = "请求URL")]
        Url = 0,
        [Display(Name = "脚本")]
        Script = 1
    }
}
